﻿Imports Amazon.S3
Imports Amazon.S3.Model
Imports System.Text


Public Class CSourceS3 : Inherits CSourceAbstract


#Region "Constructors"
    Public Sub New(Optional bucket As String = Nothing)
        Me.New(My.Settings.AwsKeyId, My.Settings.AwsKeySecret, IIf(IsNothing(bucket), My.Settings.AwsBucketName, bucket))
    End Sub
    Public Sub New(awsKeyId As String, awsSecret As String, Optional bucket As String = Nothing)
        Dim c As New AmazonS3Config()
        c.ServiceURL = "http://s3.amazonaws.com"
        c.UseHttp = True

        m_client = New AmazonS3Client(awsKeyId, awsSecret, c)
        m_currentBucket = bucket
    End Sub
#End Region

#Region "Members"
    Private m_client As AmazonS3Client
    Private m_buckets As List(Of String)
    Private m_currentBucket As String
#End Region


#Region "Properties"
    Public ReadOnly Property Client() As AmazonS3Client
        Get
            Return m_client
        End Get
    End Property
    Public ReadOnly Property Buckets As List(Of String)
        Get
            If IsNothing(m_buckets) Then
                Dim list As List(Of S3Bucket) = CAmazonS3.Buckets(Client)
                Dim names As New List(Of String)(list.Count)
                For Each i As S3Bucket In list
                    names.Add(i.BucketName)
                Next
                m_buckets = names
            End If
            Return m_buckets
        End Get
    End Property
    Public ReadOnly Property CurrentBucket As String
        Get
            If IsNothing(m_currentBucket) Then
                m_currentBucket = Buckets(0)
            End If
            Return m_currentBucket
        End Get
    End Property
    Public Sub CreateBucket(bucketName As String)
        CAmazonS3.CreateBucket(Client, bucketName)
        m_buckets = Nothing
        m_currentBucket = bucketName
    End Sub
#End Region

#Region "Overrides"
    Public Overrides ReadOnly Property Name As String
        Get
            Return String.Concat("AWS:\", CurrentBucket)
        End Get
    End Property

    'Get Info
    'Public Overrides Function GetKeys() As List(Of String)
    '    LogReadStart()
    '    Dim files As List(Of S3Object) = CAmazonS3.Objects(Client, CurrentBucket)
    '    GetKeys = Keys(files)
    '    LogReadFinish(GetKeys.Count)
    'End Function
    Public Overrides Function GetKeysAndSizes() As CKeysAndSizes
        LogReadStart()
        Dim files As List(Of S3Object) = CAmazonS3.Objects(Client, CurrentBucket, AddressOf Notify1000Loaded)
        Dim keys As CKeysAndSizes = KeysAndSizes(files)
        LogReadFinish(keys.Count, keys.Total)
        Return keys
    End Function
    Private Sub Notify1000Loaded(total As Integer)
        Log(vbTab, "Loaded ", total.ToString("n0"), " objects")
    End Sub

    'Make Changes
    Protected Overloads Overrides Sub CopyTo(destination As CSourceFolder, key As String)
        'Download
        CAmazonS3.GetObject(Client, CurrentBucket, key, destination.FullPath(key))
    End Sub
    Protected Overloads Overrides Sub CopyTo(destination As CSourceS3, key As String)
        'Download/Upload or remote copy or use EC2
        Throw New Exception
    End Sub
#End Region


#Region "AmazonS3 - Objects 2 List/Dict"
    'Public Shared Function Keys(files As List(Of S3Object)) As List(Of String)
    '    Dim list As New List(Of String)(files.Count)
    '    For Each i As S3Object In files
    '        list.Add(i.Key.ToLower)
    '    Next
    '    Return list
    'End Function
    Public Shared Function KeysAndSizes(files As List(Of S3Object)) As CKeysAndSizes
        Dim list As New CKeysAndSizes(files.Count)
		For Each i As S3Object In files
			Dim key As String = i.Key.ToLower.Replace("/", "\")
			Try
				list.Add(key, i.Size)
			Catch ex As Exception
				list(key) = i.Size
			End Try
		Next
		Return list
    End Function
#End Region


End Class
